﻿package com.wxsoft.business.physic.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.physic.dao.IPsPhysicDictDao;
import com.wxsoft.business.physic.pojo.PsPhysicDict;
import com.wxsoft.business.physic.service.IPsPhysicDictService;
import com.wxsoft.business.sys.pojo.SysUnitInfo;
import com.wxsoft.util.UnitCodeUtil;
import com.wxsoft.util.common.HanyuPinyinHelper;
import com.wxsoft.util.wx.UUIDUtil;

@Service("psPhysicDictService")
public class PsPhysicDictServiceImpl implements IPsPhysicDictService  {
	@Resource
	private IPsPhysicDictDao dao;
	
	
	public long findCount(PsPhysicDict psPhysicDict) {
		return dao.findCount(psPhysicDict);
	}
	
	public List<PsPhysicDict> findAll(PageHelper page,PsPhysicDict psPhysicDict) {
        List<PsPhysicDict> r = dao.findAll(page,psPhysicDict);
		return r;
	}
	public List<PsPhysicDict> getBy(PsPhysicDict psPhysicDict) {
        List<PsPhysicDict> r = dao.getBy(psPhysicDict);
		return r;
	}
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public String add(PsPhysicDict psPhysicDict) {
		psPhysicDict.setId(UUIDUtil.getUUID());

		this.check(psPhysicDict);
		
		//设置汉语拼音
		psPhysicDict.setMnemonic(HanyuPinyinHelper.getFirstLettersLo(psPhysicDict.getPhysic_name()));
		
		this.dao.insert(psPhysicDict);
		return "操作成功!";
	}
	
	public String modify(PsPhysicDict psPhysicDict) {

		this.check(psPhysicDict);
		
		//设置汉语拼音
		psPhysicDict.setMnemonic(HanyuPinyinHelper.getFirstLettersLo(psPhysicDict.getPhysic_name()));
		
		this.dao.modify(psPhysicDict);
		return "操作成功!";
	}
	
	public String delete(PsPhysicDict psPhysicDict) {
		this.dao.delete(psPhysicDict);
		return "操作成功!";
	}
	/**
	 * 新增和修改时校验
	 * @Description:TODO
	 * @author: chenliang
	 * @time:2018-3-18 上午11:27:07
	 */
	public void check(PsPhysicDict psPhysicDict){

		String pCode = psPhysicDict.getPhysic_code();
		PsPhysicDict oldForQ = new PsPhysicDict();
		oldForQ.setPhysic_code(pCode);
		List<PsPhysicDict> ppdL = dao.getBy(oldForQ);
		if (ppdL != null && ppdL.size() > 0) {
			throw new RuntimeException("药品编号重复，请重新输入！"
					+ psPhysicDict.getPhysic_code() + psPhysicDict.getPhysic_name());
		}
	}
	
}
